package com.mmall.seckill.common.config;

import com.mmall.seckill.common.Interceptor.UserContext;
import com.mmall.seckill.domain.SeckillUser;
import com.mmall.seckill.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 通过参数形式拿到用户信息
 */
@Slf4j
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {

    public boolean supportsParameter(MethodParameter parameter) {
        Class<?> clazz = parameter.getParameterType();
        return clazz==SeckillUser.class;
    }

    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        log.info(" 通过参数形式拿到用户信息 --> {}",JsonUtil.obj2StringPretty(UserContext.getUser()));
        return UserContext.getUser();
    }
}
